package com.aipsi.cws.auto.pages.tab5.supplier;

import com.appium.manager.element.anno.PageElement;
import com.aipsi.cws.auto.common.BasePage;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SupplierListPage extends BasePage {
	public SupplierListPage(PageElement pageElement, Class<?> curTestClass) {
		super(pageElement, curTestClass);
	}
	/**
	 * 搜索输入框
	 */
	@FindBy(css = "body > app-root > ion-app > ion-router-outlet > app-tabs > ion-tabs > div > ion-router-outlet > app-supplier > ion-header > ion-toolbar.toolbar-searchbar.md.in-toolbar.hydrated > ion-searchbar > div > input")
    private WebElement searchInput;
	/**
	 * 搜索按钮
	 */
	@FindBy(xpath = "/html/body/app-root/ion-app/ion-router-outlet/app-tabs/ion-tabs/div/ion-router-outlet/app-supplier/ion-header/ion-toolbar[2]/ion-button")
	private WebElement searchButton;
	/**
	 * 确认删除按钮
	 */
	@FindBy(css = "#ion-overlay-3 > div.alert-wrapper.ion-overlay-wrapper.sc-ion-alert-md > div.alert-button-group.sc-ion-alert-md > button.alert-button.ion-focusable.ion-activatable.confirm.sc-ion-alert-md")
	private WebElement yesButton;
	/**
	 * 否认删除按钮
	 */
	@FindBy(xpath = "//*[@id=\"ion-overlay-3\"]/div[2]/div[3]/button[1]")
	private WebElement noButton;

	@FindBy(tagName = "ion-note")
	private List<WebElement> debts;


	/**
	 * 搜索大忽悠并点击搜索按钮
	 */
	public void searchDHY() throws InterruptedException {
		log.info("搜索大忽悠并点击搜索按钮");
		searchInput.sendKeys("大忽悠");
		waitWhile(1);
		searchButton.click();
		waitWhile();
	}

	/**
	 * 选择第一个元素右滑
	 */
	public void swipe() throws InterruptedException {
         swipe(770,490,250,490);
         waitWhile(2);
	}

	public void clickYesButton() throws InterruptedException {
		clickByCoordinate(855,497);
		waitWhile();
		clickByCoordinate(654,902);
	}

	public void clickNoButton() throws InterruptedException {
		searchDHY();
		swipe();
		clickByCoordinate(855,497);
		waitWhile();
		clickByCoordinate(534,905);
	}

	/**
	 * 向上滑动
	 */
	public void swipeToTop(){
       swipe(420,480,420,1100);
	}

	/**
	 * 向上滑动,并获取当前页面所有的欠款数量,判断是否是降序排列
	 */
    public Boolean swipeToTopAndGetDebtsDesc() throws InterruptedException {
        swipeToTop();
		waitWhile();
        List<Integer> moneys=new ArrayList<>();
		for (WebElement debt : debts) {
			String innerText = debt.getAttribute("innerText");
			moneys.add(Integer.valueOf(innerText.split("：")[1]));
		}
		return isDesc(moneys);
	}

	private Boolean isDesc(List<Integer> moneys) {
	     Integer prev=moneys.get(0);
		for (Integer money : moneys) {
			if(prev<money){
				return false;
			}
		}
		return true;
	}
}
